﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace SMS.dao
{
    public class PreferenceDao
    {
        private static readonly PreferenceDao INSTANCE = new PreferenceDao();

        public static PreferenceDao Instance
        {
            get
            {
                return INSTANCE;
            }
        }

        private PreferenceDao() { }

        public DataTable findByCode(String code)
        {
            String sql = "select pre_value " +
                         "from preference " +
                         "where pre_code = @preCode";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@preCode", code));
            return DataFetcher.Instance.fetch();
        }

        //long
        public int update(String value, String code)
        {
            String sql = "update preference set pre_value = @value where pre_code = @code";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@value", Int32.Parse(value)));
            DataFetcher.Instance.addParameter(new SqlParameter("@code", code));
            return DataFetcher.Instance.execute();
        }
        
        public DataTable findAll()
        {
            String sql = "select * from preference " +
                         "order by pre_code";
            DataFetcher.Instance.setCommand(sql, 1);
            return DataFetcher.Instance.fetch();
        }

        public ArrayList findById(int id)
        {
            String sql = "select pre_code, pre_name, pre_value from preference where pre_id = @id";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@id", id));
            return DataFetcher.Instance.getOneRow();
        }
        //end of long
    }
}
